package com.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.web.dao.AdminUserDao;
import com.web.entity.AdminUser;
import com.web.entity.SysUser;
import com.web.service.AdminUserService;

@Service
public class AdminUserServiceImpl implements AdminUserService {

	@Autowired
	private AdminUserDao adminUserDao;

	@Autowired
	private HttpSession session;

	@Override
	public Map<String, Object> doAdminUserLogin(String username, String password) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
		List<AdminUser> adminUserlist = adminUserDao.getAdminUSerLogin(username, password);
		System.out.println(md5Password);
		if (adminUserlist.size() == 1) {
			System.out.println(adminUserlist.size() + "##################");
			if (md5Password.equals(adminUserlist.get(0).getPassword())) {
				resultMap.put("result", 1);
				this.session.setAttribute("adminUser", adminUserlist);
				this.session.setAttribute("path", adminUserlist.get(0).getPath());
			} else {
				resultMap.put("result", 3);
			}
		}
		if (adminUserlist.size() == 0) {
			resultMap.put("result", 0);
		}
		if (adminUserlist.size() > 1) {
			resultMap.put("result", 2);
		}
		return resultMap;
	}

	@Override
	public List<AdminUser> secletAdminUser(String username) {
		// TODO Auto-generated method stub
		return adminUserDao.getAdminUser(username);
	}

	@Override
	public Map<String, Object> updateAdminInfo(AdminUser adminUser) {
		Map<String, Object> map = new HashMap<String, Object>();
		adminUserDao.updateAdminInfo(adminUser);
		map.put("result", 1);
		return map;
	}

}
